package com.shujia.core

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo4EcentTIme {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //因为每一个task中是单独计算水位线，如果数据量小，会导致部分task水位线增大，大部分的task水位线还很小
    // 当所有task中的水位线都超过窗口的结束时间之后才会计算
    env.setParallelism(1)

    //设置flink的时间模式为事件时间
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

    /*

001,1637893735000
001,1637893736000
001,1637893737000
001,1637893739000
001,1637893740000
001,1637893738000
001,1637893741000
001,1637893745000
001,1637893745000
001,1637893746000
001,1637893747000
001,1637893752000
     */

    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092")
    properties.setProperty("group.id", "asdasdsa")

    //创建flink kafka 消费者
    val flinkKafkaConsumer = new FlinkKafkaConsumer[String]("words", new SimpleStringSchema(), properties)


    flinkKafkaConsumer.setStartFromLatest()


    val kafkaDS: DataStream[String] = env.addSource(flinkKafkaConsumer)


    val eventDS: DataStream[(String, Long)] = kafkaDS.map(line => {
      val split: Array[String] = line.split(",")
      (split(0), split(1).toLong)
    })
      //告诉flink数据中哪一个字段是时间字段
      // .assignAscendingTimestamps(_._2)
      //设置时间字段和水位线
      .assignTimestampsAndWatermarks(
      //水位线默认等于时间戳最大数据的时间
      //将水位线前移，剞劂数据乱序问题
      new BoundedOutOfOrdernessTimestampExtractor[(String, Long)](Time.seconds(5)) {
        //返回事件时间字段
        override def extractTimestamp(element: (String, Long)): Long = {
          element._2
        }
      }
    )

    /**
      * 每个5秒统计每一个id出现的次数
      *
      */


    eventDS.map(kv => (kv._1, 1))
      .keyBy(_._1)
      .timeWindow(Time.seconds(5))
      .sum(1)
      .print()


    env.execute()


  }

}
